package com.ran.servers.server;

import com.ran.servers.Container.Container;
import com.ran.servers.ServerConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

/**
 * 
 * @Title: NettyServer
 * @Description: Netty服务端
 * @Version:1.0.0
 * @author ran
 * @date 2024/5/08 10:00
 */
@Component
public class NettyServer implements Container {
	private static Logger logger = LoggerFactory.getLogger(NettyServer.class);
	private static final int port = 8008; // 设置服务端端口
	private static EventLoopGroup boss = new NioEventLoopGroup(); // 通过nio方式来接收连接和处理连接
	private static EventLoopGroup work = new NioEventLoopGroup(); // 通过nio方式来接收连接和处理连接
	private static ServerBootstrap b = new ServerBootstrap();

	@Resource
	private ServerConfig serverConfig;
	@Resource
	private NettyServerFilter nettyServerFilter;

	public void start() {
		try {
			serverConfig.Init();
			b.group(boss, work);
			b.channel(NioServerSocketChannel.class);
			b.childHandler(nettyServerFilter); // 设置过滤器
			// 服务器绑定端口监听
			ChannelFuture f = b.bind(port).sync();
			System.out.println("服务端启动成功,端口是:" + port);
			// 监听服务器关闭监听
			f.channel().closeFuture().sync();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void stop() {
		// 关闭EventLoopGroup，释放掉所有资源包括创建的线程
		work.shutdownGracefully();
		boss.shutdownGracefully();
	}
}
